﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Xml;

namespace WebViecLam
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private static string enrollInforID = "";
        private static string userID = "";
        private static string _lat = "";
        private static string _lng = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Headers["ViewInterviewPlaces"] != null)
            {
                string strXML = ConvertInterviewPlaceToXMLString();

                Response.ContentType = "text/xml";
                Response.Write(strXML);
                Response.End();
            }

            if(!IsPostBack)
            {
                string strRequest = "http://localhost:8080/ViecLamService/GetItemEnrollInformation/" + Request.QueryString["id"];
                HttpWebRequest request = (HttpWebRequest)(WebRequest.Create(strRequest));
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader streamReader = new StreamReader(response.GetResponseStream());

                // string xml
                string xmlListOfStudents = streamReader.ReadToEnd();

                if (xmlListOfStudents != "")
                {
                    XmlDocument xmlDocument = new XmlDocument();
                    xmlDocument.LoadXml(xmlListOfStudents);

                    XmlNode nodeDS = xmlDocument.DocumentElement;

                    enrollInforID = nodeDS.ChildNodes[0].InnerText;
                    lbEnrName.Text = nodeDS.ChildNodes[1].InnerText;
                    lbEnrDescription.Text = nodeDS.ChildNodes[2].InnerText;
                    lbEnrRequire.Text = nodeDS.ChildNodes[3].InnerText;
                    userID = nodeDS.ChildNodes[4].InnerText;
                    lbCompanyName.Text = nodeDS.ChildNodes[5].InnerText;
                    lbCompanyDescription.Text = nodeDS.ChildNodes[6].InnerText;
                    lbCompanyLink.Text = nodeDS.ChildNodes[7].InnerText;
                    lbLevel.Text = nodeDS.ChildNodes[8].InnerText;
                    lbEnrAge.Text = nodeDS.ChildNodes[9].InnerText;
                    lbSex.Text = nodeDS.ChildNodes[10].InnerText;
                    lbDiploma.Text = nodeDS.ChildNodes[11].InnerText;
                    lbEnglishLevel.Text = nodeDS.ChildNodes[12].InnerText;
                    lbExperienceNumber.Text = nodeDS.ChildNodes[13].InnerText;
                    lbQuantity.Text = nodeDS.ChildNodes[14].InnerText;
                    lbSalary.Text = nodeDS.ChildNodes[15].InnerText;
                    lbMajor.Text = nodeDS.ChildNodes[16].InnerText;
                    string isShow = nodeDS.ChildNodes[17].InnerText;
                    lbCreatedDate.Text = nodeDS.ChildNodes[18].InnerText;
                    lbEndDate.Text = nodeDS.ChildNodes[19].InnerText;
                    lbArea.Text = nodeDS.ChildNodes[20].InnerText;
                    _lat = nodeDS.ChildNodes[21].InnerText;
                    _lng = nodeDS.ChildNodes[22].InnerText;
                    lbLeaderName.Text = nodeDS.ChildNodes[23].InnerText;
                    lbLeaderPhone.Text = nodeDS.ChildNodes[24].InnerText;
                    lbLeaderEmail.Text = nodeDS.ChildNodes[25].InnerText;
                }
            }
        }

        private string ConvertInterviewPlaceToXMLString()
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\" ?> <WebsiteGioiThieuViecLam></WebsiteGioiThieuViecLam>");

            XmlElement node;
            node = doc.CreateElement("InterviewPlace");

            node.SetAttribute("Lat", _lat);
            node.SetAttribute("Lng", _lng);

            doc.DocumentElement.AppendChild(node);

            return doc.InnerXml;
        }

        protected void btnSendRecord_Click(object sender, EventArgs e)
        {
            Response.Redirect("NopDonTrucTuyen.aspx?userID=" + userID + "&enrollID=" + enrollInforID);
        }
    }
}